package vashion.azeroth.core.service.impl;

import java.util.ArrayList;
import java.util.Date;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import vashion.azeroth.contant.GenericErrorEnum;
import vashion.azeroth.contant.MemberErrorEnum;
import vashion.azeroth.core.manager.personal.PersonalFriendManager;
import vashion.azeroth.core.manager.personal.PersonalMemberManager;
import vashion.azeroth.core.personal.pojo.PersonalFriend;
import vashion.azeroth.core.personal.pojo.PersonalMember;
import vashion.azeroth.core.result.ApiResult;
import vashion.azeroth.core.service.PersonalFriendService;

@Service
public class PersonalFriendServiceImpl<T> implements
		PersonalFriendService<T> {
	
	private final static Logger log = LoggerFactory
			.getLogger(PersonalFriendServiceImpl.class);

	@Autowired
	private PersonalFriendManager personalFriendManager;

	@Autowired
	private PersonalMemberManager personalMemberManager;
	
	
	public ApiResult<ArrayList<PersonalFriend>> getFirstLevelFriends(PersonalMember member) {
		ApiResult<ArrayList<PersonalFriend>> result = new ApiResult<ArrayList<PersonalFriend>>();
		try {
			//一级人脉不区分上下级
			ArrayList<PersonalFriend> firLevFriendList = personalFriendManager.getFirstLevelFriends(member);
			if (null == firLevFriendList || firLevFriendList.size() == 0) {
				return ApiResult.newErrorResult(MemberErrorEnum.FIRST_LEVEL_NULL.getErrorCode(), MemberErrorEnum.FIRST_LEVEL_NULL.getErrorMessage());
			}
			
			int totalCounts = firLevFriendList.size();
			PersonalFriend higherMember = new PersonalFriend();
			for (int i = 0; i < firLevFriendList.size(); i++) {
				firLevFriendList.get(i).getMemberId();
				if (member.getMemberId() == firLevFriendList.get(i).getMemberId()) {
					higherMember.setMemberId(firLevFriendList.get(i).getInviteMemberId());
					firLevFriendList.remove(i);
				}
			}
			firLevFriendList.add(higherMember);
			
			result = ApiResult.newSuccessResult(firLevFriendList, totalCounts);
		} catch (Exception e) {
			System.out.println(e);
		}
		return result;
	}


	public int findCntForFirLevelFridends(PersonalMember member) {
		int count = personalFriendManager.findCntForFirLevelFridends(member);
		return count;
	}

	/**
	 * 人脉测试case
	 * 一级人脉case：
	 * 有上线无下线
	 * 有上线有下线
	 * 无上线有下线
	 * 无上线无下线（无一级人脉）
	 * 
	 * 二级人脉case
	 * 上线有下线
	 * 上线无下线
	 * 上线无下线，下线无下线
	 * 上线无下线，下线有下线
	 * 上线有下线，下线无下线
	 * 上线有下线，下线有下线
	 * 下线无下线
	 * 下线有下线
	 * 无二级人脉
	 * 
	 * 
	 * */
	public ApiResult<ArrayList<PersonalFriend>> getSecondLevelFriends(
			PersonalMember member) {
		ApiResult<ArrayList<PersonalFriend>> result = new ApiResult<ArrayList<PersonalFriend>>();
		ArrayList<PersonalFriend> secLevFriList = new ArrayList<PersonalFriend>();
		try {
			// 取我上线的下线
			ArrayList<PersonalFriend> secLevFriList1 = personalFriendManager
					.getSecLevelFriends1(member);
			if (null == secLevFriList1 || secLevFriList1.size() == 0) {
				// 获取上线的下线为空，则直接去获取下线的下线
				ArrayList<PersonalFriend> secLevFriList2 = personalFriendManager
						.getSecLevelFriends2(member);
				if (null == secLevFriList2 || secLevFriList2.size() == 0) {
					return ApiResult
							.newErrorResult(MemberErrorEnum.SECOND_LEVEL_NULL
									.getErrorCode(),
									MemberErrorEnum.SECOND_LEVEL_NULL
											.getErrorMessage());
				} else {
					for (int i = 0; i < secLevFriList2.size(); i++) {
						secLevFriList.add(secLevFriList2.get(i));
					}
				}
				int totalCounts = secLevFriList.size();
				result = ApiResult.newSuccessResult(secLevFriList, totalCounts);
				return result;

			} else {
				for (int i = 0; i < secLevFriList1.size(); i++) {
					if (secLevFriList1.get(i).getMemberId() == member
							.getMemberId()) {
						;
					} else {
						secLevFriList.add(secLevFriList1.get(i));
					}
				}
			}
			
			//获取下线的下线
			ArrayList<PersonalFriend> secLevFriList2 = personalFriendManager
					.getSecLevelFriends2(member);
			if (null == secLevFriList2 || secLevFriList2.size() == 0) {
				;
			} else {
				for (int i = 0; i < secLevFriList2.size(); i++) {
					secLevFriList.add(secLevFriList2.get(i));
				}
			}
			int totalCounts = secLevFriList.size();
			result = ApiResult.newSuccessResult(secLevFriList, totalCounts);
		} catch (Exception e) {
			System.out.println(e);
		}
		return result;
	}
	
	/**
	 * 本方法用于获取用户下线的下线
	 * 
	 * */
	public ArrayList<PersonalFriend> getFriends2(PersonalMember member) {
			ArrayList<PersonalFriend> secLevFriList2 = personalFriendManager
					.getSecLevelFriends2(member);
			if (null == secLevFriList2 || secLevFriList2.size() == 0) {
				return null;
			} else {
				for (int i = 0; i < secLevFriList2.size(); i++) {
					secLevFriList2.add(secLevFriList2.get(i));
				}
			}
			return secLevFriList2;
	}

	
	public ApiResult<PersonalFriend> saveFriend(
			PersonalMember member) {
		ApiResult<PersonalFriend> result = new ApiResult<PersonalFriend>();
		PersonalFriend friend = new PersonalFriend();
		try {
			if (null == member.getMemberId() || StringUtils.isBlank(String.valueOf(member.getMemberId()))) {
				return ApiResult.newErrorResult(
						GenericErrorEnum.NOT_EMPTY.getErrorCode(), "会员ID"
								+ GenericErrorEnum.NOT_EMPTY.getErrorMessage());
			}
			
			PersonalMember memberInfo = personalMemberManager.get(member.getMemberId());
			if (null == memberInfo || StringUtils.isBlank(String.valueOf(member.getMemberId()))) {
				return ApiResult.newErrorResult(MemberErrorEnum.PROIFLES_NODATA.getErrorCode(), MemberErrorEnum.PROIFLES_NODATA.getErrorMessage());
			}
			
			if (null == member.getInviteMemberId() || StringUtils.isBlank(String.valueOf(member.getInviteMemberId()))) {
				friend.setInviteMemberId(member.getInviteMemberId());
				friend.setInviteMemberName("");
			}
			
			if (null == member.getMemberName() || StringUtils.isBlank(String.valueOf(member.getMemberName()))) {
				friend.setMemberName(memberInfo.getMemberName());
			}
			
			friend.setFlag(1);
			friend.setMemberId(member.getMemberId());
			friend.setTimeAdd(new Date(System.currentTimeMillis()));
			friend.setTimeEdit(new Date(System.currentTimeMillis()));
			friend.setInviteFeedCount(0);
			friend.setInviteItemCount(0);
			friend.setItemCount(0);
			friend.setMemberItemCount(0);
			friend.setMemberFeedCount(0);
			
			//添加人脉
			PersonalFriend friendRes = personalFriendManager.save(friend);
			if (null == friendRes) {
				;
			} else {

			int totalCounts = 1;
			result = ApiResult.newSuccessResult(friendRes, totalCounts);
			}
		} catch (Exception e) {
			log.error("添加人脉失败" + e);
		}
		return result;
	}


}
